//禁止移动设备双击放大
var lastTouchEnd = 0;
document.documentElement.addEventListener('touchend', function (event) {
    var now = Date.now();
    if (now - lastTouchEnd <= 300) {
        event.preventDefault();
    }
    lastTouchEnd = now;
}, false);

//代码高亮显示插件
hljs.highlightAll();

//代码行号显示插件
hljsln.initLineNumbersOnLoad();

//layui组件
layui.use(['layer', 'util'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var posting = 0;

    //右下角返回顶部图标
    util.fixbar();

    //文章图片点击放大
    layer.photos({
        photos: '.post-container'
    });

    //移动设备导航菜单切换
    $('.toggle-navigation').click(function () {
        $('.menu-container').slideToggle();
    });

    //个人介绍，因为只显示2行，点击弹出显示全部
    $('.profile-intro .intro').click(function () {
        var str = $(this).text();
        str = '<p style="padding:1em;max-width:470px">' + str + '</p>';
        layer.open({
            type: 1,
            shadeClose: true,
            title: false,
            closeBtn: 0,
            content: str
        });
    });

    //提交评论
    $('#comment-form').submit(function () {
        var data = {};
        data.article_id = $("input[name=article_id]", $(this)).val();
        data.content = $("textarea[name=content]", $(this)).val();
        data.name = $("input[name=name]", $(this)).val();
        data.email = $("input[name=email]", $(this)).val();
        data.url = $("input[name=url]", $(this)).val();
        data.avatar = $("input[name=avatar]:checked", $(this)).val();

        var emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
        var urlReg = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;

        if (data.content.length < 5) {
            layer.msg('评论字数太少', {icon: 5});
            return false;
        }
        if ($.trim(data.name).length == 0) {
            layer.msg('请填写您的名字', {icon: 5});
            return false;
        }
        if (emailReg.test(data.email) == false) {
            layer.msg('邮箱地址格式有误', {icon: 5});
            return false;
        }
        if (data.url.length > 0 && urlReg.test(data.url) == false) {
            layer.msg('您的主页地址不符合规范', {icon: 5});
            return false;
        }

        // new TencentCaptcha(CaptchaAppId, function (res) {
        //     if (res.ret !== 0) {
        //         return;
        //     }
        //
        //     data.captcha_ticket = res.ticket;
        //     data.captcha_randstr = res.randstr;

            $.post('/comment', data, function (res) {
                posting = 0;
                if (res.code === 0) {
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    }, function () {
                        location.href = '#comments';
                        location.reload();
                    });
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }, 'json');
        // }).show();

        return false;
    });

    //选择头像时的勾选效果
    $('.comment-form-avatar input[name=avatar]').change(function () {
        if ($(this).prop('checked')) {
            $(this).parent().addClass('checked');
            $(this).parent().siblings().removeClass('checked');
        }
    });

    //显示复制代码按钮
    $('pre code').parent().append('<button type="button" class="layui-btn layui-btn-sm layui-btn-primary copy-btn">复制</button>');
    var clipboard = new ClipboardJS('.copy-btn', {
        text: function (trigger) {
            return $(trigger).siblings('code').text();
        }
    });
    clipboard.on('success', function () {
        layer.msg('复制成功', {icon: 1});
    });
    clipboard.on('error', function () {
        layer.msg('复制失败，请手动复制！', {icon: 5});
    });
});
